package com.eian.boot.mybatis.core.service;

import com.eian.boot.mybatis.core.mapper.IMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;

import java.io.Serializable;
import java.util.Collection;

/**
 * @author alex.meng
 * @createTime 2024-06-02 19:09
 * @description 项目基础Service接口实现
 **/
public class BaseService<M extends IMapper<T>, T> extends ServiceImpl<M, T>
        implements IBaseService<T> {

    @Override
    public boolean saveBatchRecords(Collection<T> entities) {
        return getMapper().insertBatch(entities) > 0;
    }

    @Override
    public int removeBatch(QueryWrapper query) {
        return getMapper().deleteByQuery(query);
    }

    @Override
    public boolean removeIfExists(QueryWrapper query) {
        boolean exists = exists(query);
        if (!exists) {
            return true;
        }
        return getMapper().deleteByQuery(query) > 0;
    }

    // ===== 物理删除（在逻辑删除场景下使用）=====
    @Override
    public boolean deleteByIdForce(Serializable id) {
        return getMapper().deleteByIdForce(id) > 0;
    }

    @Override
    public boolean removeByIdsForce(Collection<? extends Serializable> ids) {
        return getMapper().deleteByIdsForce(ids) > 0;
    }

    @Override
    public boolean removeBatchByIdsForce(Collection<? extends Serializable> ids, int size) {
        return getMapper().deleteByIdsForce(ids, size) > 0;
    }

    @Override
    public int removeByQueryForce(QueryWrapper queryWrapper) {
        return getMapper().deleteByQueryForce(queryWrapper);
    }
}
